<template>
  <el-dialog
    :title="type === 'add' ? '新增用户' : '编辑用户'"
    :visible.sync="isShow"
    center
    width="600px"
    @close="closeEvent"
    custom-class="userDialog"
  >
    <el-form ref="form" :model="form" label-width="120px" :rules="rules">
      <el-form-item label="用户名称" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="role_id">
        <el-select placeholder="请选择" v-model="form.role_id">
          <el-option label="超级管理员" value="1"></el-option>
          <el-option label="管理员" value="2"></el-option>
          <el-option label="老师" value="3"></el-option>
          <el-option label="学生" value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select placeholder="请选择" v-model="form.status">
          <el-option label="启用" :value="1"></el-option>
          <el-option label="禁用" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户备注" prop="remark">
        <el-input v-model="form.remark"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="isShow = false">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { userAdd, userEdit } from '@/api/user.js'
export default {
  props: ['type'],
  data () {
    return {
      isShow: false,
      form: {
        username: '', // 用户名
        email: '', // 邮箱
        phone: '', // 手机号
        role_id: '', // 角色 、2 管理员、3 老师、4 学生
        status: 1, // 1(启用)0(禁用)
        remark: '' // 备注
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 50,
            message: '长度在 3 到 50个字符',
            trigger: 'blur'
          }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' },
          {
            validator: (rules, value, callback) => {
              const reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正确的邮箱地址'))
              }
            },
            trigger: 'blur'
          }
        ],
        phone: [
          { required: true, message: '必填', trigger: 'blur' },
          // 限制长度
          {
            min: 11,
            max: 11,
            message: '请输入正确手机号码',
            trigger: 'blur'
          },
          {
            validator: (rules, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正确手机号码'))
              }
            },
            trigger: 'blur'
          }
        ],
        role_id: [{ required: true, message: '请选择角色', trigger: 'blur' }],
        status: [{ required: true, message: '请选择状态', trigger: 'blur' }]
      }
    }
  },
  methods: {
    submit () {
      this.$refs.form.validate(async (result) => {
        if (result) {
          if(this.type === 'add') {
            await userAdd(this.form)
            this.$message.success('新增成功')
            this.$parent.pageChange(1)
          } else {
            await userEdit(this.form)
            this.$message.success('编辑成功')
            this.$parent.getData()
          }
          this.isShow = false
        }
      })
    },
    closeEvent () {
      this.$refs.form.resetFields()
      this.form = {
        username: '', // 用户名
        email: '', // 邮箱
        phone: '', // 手机号
        role_id: '', // 角色 、2 管理员、3 老师、4 学生
        status: 1, // 1(启用)0(禁用)
        remark: '' // 备注
      }
    },
    initForm(data){
      Object.keys(this.form).forEach(item => {
        this.form[item] = data[item]
      })
      this.form.id = data.id
    }
  }
}
</script>

<style lang="less">
.userDialog {
  .el-dialog__header {
    height: 53px;
    text-align: center;
    line-height: 53px;
    color: #fff;
    background: linear-gradient(to right, #01c5fa, #1394fa);
    padding: 0;
    .el-dialog__title {
      font-size: 16px;
      color: #fff;
    }
    .el-icon-close:before {
      content: '\e6db';
      color: #fff;
    }
  }
}
</style>
